package demo.tellurium.framework;

import org.apache.commons.lang.StringUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.telluriumsource.test.java.TelluriumJUnitTestCase;

import demo.util.BaseUtils;
import demo.util.Console;

public abstract class BaseTelluriumJUnitTestCase extends TelluriumJUnitTestCase {

    protected static final int WAITING_DELAY_SEC = 1;

    protected static Console console = BaseUtils.getConsole();

    @BeforeClass
    public static void init() {

        // connect server
        connectSeleniumServer();

        // load properties
        loadProperties();

        // configuration
        config();
    }

    @AfterClass
    public static void end() {
        manualBreak();
    }

    static void config() {
        useCssSelector(true);
        useTelluriumEngine(false);
        useTelluriumApi(false);
        useTrace(false);
        useEngineLog(false);//
    }

    static void loadProperties() {
        BaseUtils.loadProperties();
    }

    protected static String getProperty(String key) {
        return BaseUtils.getProperty(key);
    }

    protected static void waitingDelay() {
        pauseSec(WAITING_DELAY_SEC);
    }

    protected static void pauseSec(int seconds) {
        pause(seconds * 1000);
    }

    protected static <T> T cast(Object obj) {
        return BaseUtils.cast(obj);
    }

    protected static void manualBreak() {
        String cmd = console.readLine();
        if (StringUtils.endsWithIgnoreCase(cmd, "exit") || StringUtils.endsWithIgnoreCase(cmd, "quit")) {
            throw new RuntimeException("User has stopped this operation.");
        }
    }

}
